﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.houseWork.Helper;

namespace CIT.WMS.input
{
    public partial class ChooseMaterial : Form
    {
        bool flag = false;
        bool result = false;
        public ChooseMaterial()
        {
            InitializeComponent();
            databind();
        }

        private void databind()
        {
            string strSql = string.Format(@"SELECT IQC_NO FROM dbo.T_Bllb_IQCDoc_tbid WHERE CHECK_RESULT='5' ORDER BY IQC_NO DESC");
            DataTable dt_po = Utils.NMS.QueryDataTable(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0]);
            DataRow dr = dt_po.NewRow();
            dr["IQC_NO"] = string.Empty;
            dt_po.Rows.InsertAt(dr, 0);
            cbo_IQC_NO.DisplayMember = "IQC_NO";
            cbo_IQC_NO.ValueMember = "IQC_NO";
            cbo_IQC_NO.DataSource = dt_po;
         
        }

        private void txt_reelid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                //ClearControl();
                string strSql = string.Format(@"SELECT TOP 1 B.PO,B.MaterialCode,A.QTY,A.ChooseFlag,B.QTY-B.REJECT_QTY AS R_QTY FROM dbo.T_Bllb_IQCProduct_tbip A LEFT JOIN dbo.T_Bllb_IQCDoc_tbid B ON A.IQC_NO=B.IQC_NO WHERE A.IQC_NO='{0}' AND A.SERIAL_NUMBER='{1}'", cbo_IQC_NO.Text.Trim(), txt_reelid.Text.Trim());
                DataTable dt_p = Utils.NMS.QueryDataTable(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0]);
                if (dt_p.Rows.Count == 0)
                {
                    lbl_msg.Text = "当前IQC检验单中不存在此物料";
                    lbl_msg.ForeColor = Color.Red;
                    return;
                }
                else
                {

                    if (SqlInput.ChangeNullToString(dt_p.Rows[0]["ChooseFlag"]) == "Y")
                    {
                        lbl_msg.Text = "物料已挑选过";
                        lbl_msg.ForeColor = Color.Red;
                        return;
                    }
              
        
                    if (SqlInput.ChangeNullToInt(lbl_R_Qty.Text, 0) < SqlInput.ChangeNullToInt(dt_p.Rows[0]["QTY"], 0) + SqlInput.ChangeNullToInt(lbl_TotalQty.Text, 0))
                    {
                        lbl_msg.Text = "该物料数量超过剩余可挑选的数量";
                        lbl_msg.ForeColor = Color.Red;
                        return;
                    }
                    strSql = string.Format(@"UPDATE T_Bllb_IQCProduct_tbip SET ChooseFlag='Y' WHERE IQC_NO='{0}' AND SERIAL_NUMBER='{1}'", cbo_IQC_NO.Text.Trim(), txt_reelid.Text.Trim());
                    Utils.NMS.Exectransql(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0], out flag, out result);
                    strSql = string.Format(@"SELECT SUM(QTY) as totalQty FROM dbo.T_Bllb_IQCProduct_tbip WHERE IQC_NO='{0}' AND ChooseFlag='Y'", cbo_IQC_NO.Text.Trim());
                    DataTable dt_total = Utils.NMS.QueryDataTable(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0]);
                    lbl_TotalQty.Text = SqlInput.ChangeNullToInt(dt_total.Rows[0]["totalQty"], 0).ToString();//已挑选总数
                    lbl_MaterialCode.Text = dt_p.Rows[0]["MaterialCode"].ToString();//物料
                    lbl_PO.Text = dt_p.Rows[0]["PO"].ToString();//采购单
                    lbl_QTY.Text = dt_p.Rows[0]["QTY"].ToString();//当前物料数量
                    lbl_R_Qty.Text = dt_p.Rows[0]["R_QTY"].ToString();//计划挑选总数

                    lbl_msg.Text = "挑选成功";
                    lbl_msg.ForeColor = Color.Green;
                    return;

                }

            }
        }

        private void cbo_PO_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearControl();
        }

        private void ClearControl()
        {
            lbl_MaterialCode.Text = string.Empty;
            lbl_PO.Text = string.Empty;
            lbl_QTY.Text = string.Empty;
            lbl_TotalQty.Text = string.Empty;
            lbl_msg.Text = string.Empty;
            lbl_R_Qty.Text = string.Empty;
        }

        private void cbo_PO_LostFocus(object sender, EventArgs e)
        {
            if (cbo_IQC_NO.Text.Trim() != string.Empty)
            {
                string strSql = string.Format(@"SELECT B.PO,B.MaterialCode,B.QTY-B.REJECT_QTY AS R_QTY,SUM( CASE ChooseFlag WHEN 'Y' THEN A.QTY ELSE 0 END)  AS totalQty 
FROM dbo.T_Bllb_IQCProduct_tbip A LEFT JOIN 
dbo.T_Bllb_IQCDoc_tbid B ON A.IQC_NO=B.IQC_NO WHERE A.IQC_NO='{0}'
GROUP BY B.PO,B.MaterialCode,B.QTY,B.REJECT_QTY", cbo_IQC_NO.Text.Trim());
                DataTable dt_po = Utils.NMS.QueryDataTable(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0]);
                if (dt_po.Rows.Count == 0)
                {
                    lbl_msg.Text = "IQC检验单不存在";
                    lbl_msg.ForeColor = Color.Red;
                    return;
                }
                //else if (SqlInput.ChangeNullToString(dt_po.Rows[0][0]) != "5")
                //{
                //    lbl_msg.Text = cbo_IQC_NO.Text.Trim() + "不是审核结果为挑选的IQC检验单";
                //    lbl_msg.ForeColor = Color.Red;
                //    return;
                //}
                lbl_MaterialCode.Text = dt_po.Rows[0]["MaterialCode"].ToString();//物料
                lbl_PO.Text = dt_po.Rows[0]["PO"].ToString();//采购单
                lbl_TotalQty.Text = dt_po.Rows[0]["totalQty"].ToString();//已挑选总数
                lbl_R_Qty.Text = dt_po.Rows[0]["R_QTY"].ToString();//计划挑选总数
                lbl_msg.Text = "IQC检验单设置OK";
                lbl_msg.ForeColor = Color.Green;
                return;
            
            }
        }
    }
}